package cn.huangsm.advance.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;


/**
 * 测试OSS
 *
 * @author huang
 * @PACKAGE_NAME cn.huangsm.advance.oss
 * @PROJECT_NAME advance-code
 * @date 2019/1/15
 */
@RestController
public class OSSController {

    @Autowired
    private OSSProperties ossProperties;

    //@Autowired
    private OSS client;

    @GetMapping("/upload")
    public String testUpload() throws IOException {
        String filepath = "C:\\Users\\h1261\\Desktop\\校园物流管理系统架构.png";
        File file = new File(filepath);
        String eTag=null;
        if (file != null) {
            InputStream inputStream = new FileInputStream(file);
            PutObjectResult putObjectResult = client.putObject(ossProperties.getBucketName(), ossProperties.getDirName()+"/"+ LocalDateTime.now()+".png", inputStream);
            eTag = putObjectResult.getETag();
        }
        return eTag;
    }
    @GetMapping("/download")
    public String testDownload(){
        ObjectMetadata object = client.getObject(new GetObjectRequest(ossProperties.getBucketName(), ossProperties.getDirName()+"/校园物流管理系统架构.png"), new File("C:\\Users\\h1261\\Desktop\\呵呵大兄弟.png"));
        return object.toString();
    }
}
